package com.ruoyi.train.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.train.domain.TrainPlan;
import com.ruoyi.train.domain.Vo.DictVo;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.train.mapper.TrainPlanMapper;
import com.ruoyi.train.service.ITrainPlanService;

/**
 * 培训计划Service业务层处理
 * 
 * @author wusihao
 * @date 2023-03-16
 */
@Service
public class TrainPlanServiceImpl extends ServiceImpl<TrainPlanMapper, TrainPlan> implements ITrainPlanService {
    @Autowired
    private TrainPlanMapper trainPlanMapper;

    @Autowired
    private ITrainPlanService trainPlanService;

    @Autowired
    private ISysDictDataService dictDataService;


    /**
     * 查询培训计划
     *
     * @param id 培训计划主键
     * @return 培训计划
     */
    @Override
    public TrainPlan selectTrainPlanById(Long id) {
        TrainPlan trainPlan = trainPlanMapper.selectTrainPlanById(id);
        return trainPlan;
    }

    /**
     * 查询培训计划列表
     *
     * @param trainPlan 培训计划
     * @return 培训计划
     */
    @Override
    public List<TrainPlan> selectTrainPlanList(TrainPlan trainPlan) {
        QueryWrapper<TrainPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(trainPlan.getTrainType() != null, "train_type", trainPlan.getTrainType());
        queryWrapper.eq(trainPlan.getTrainMethod() != null, "train_method", trainPlan.getTrainMethod());
        queryWrapper.eq(trainPlan.getQualifiedType() != null, "qualified_type", trainPlan.getQualifiedType());
        queryWrapper.eq("is_deleted", 0);
        queryWrapper.and(temp -> temp.eq("is_pass", 0).or().eq("is_pass", -1).or().eq("is_pass", 1));
        queryWrapper.and(temp -> temp.eq("is_submit", 0).or().eq("is_submit", -1).or().eq("is_submit", 1));

        List<TrainPlan> trainPlans = trainPlanService.list(queryWrapper);
        return trainPlans;
    }

    /**
     * 新增培训计划
     *
     * @param trainPlan 培训计划
     * @return 结果
     */
    @Override
    public int insertTrainPlan(TrainPlan trainPlan) {
        trainPlan.setCreateId(SecurityUtils.getUserId());
        trainPlan.setCreateTime(DateUtils.getNowDate());
        return trainPlanMapper.insertTrainPlan(trainPlan);
    }

    /**
     * 修改培训计划
     *
     * @param trainPlan 培训计划
     * @return 结果
     */
    @Override
    public int updateTrainPlan(TrainPlan trainPlan) {
        trainPlan.setUpdateId(SecurityUtils.getUserId());
        trainPlan.setUpdateTime(DateUtils.getNowDate());
        return trainPlanMapper.updateTrainPlan(trainPlan);
    }

    /**
     * 批量删除培训计划
     *
     * @param ids 需要删除的培训计划主键
     * @return 结果
     */
    @Override
    public int deleteTrainPlanByIds(Long[] ids) {
        for (int i = 0; i < ids.length; i ++ ) {
            Long id = ids[i];
            TrainPlan trainPlan = trainPlanMapper.selectById(id);
            trainPlan.setUpdateId(SecurityUtils.getUserId());
            trainPlanMapper.updateById(trainPlan);
        }

        return trainPlanMapper.deleteTrainPlanByIds(ids);
    }

    /**
     * 删除培训计划信息
     *
     * @param id 培训计划主键
     * @return 结果
     */
    @Override
    public int deleteTrainPlanById(Long id) {
        TrainPlan trainPlan = trainPlanMapper.selectById(id);
        trainPlan.setUpdateId(SecurityUtils.getUserId());
        trainPlanMapper.updateById(trainPlan);
        return trainPlanMapper.deleteTrainPlanById(id);
    }

    @Override
    public int submitTrain(Long trainId) {
        TrainPlan trainPlan = trainPlanMapper.selectById(trainId);
        trainPlan.setApplyUnitId(String.valueOf(SecurityUtils.getUserId()));
        trainPlan.setIsSubmit(1);
        trainPlan.setUpdateId(SecurityUtils.getUserId());
        return trainPlanMapper.updateTrainPlan(trainPlan);
    }

    @Override
    public List<TrainPlan> selectTrainPlanVoList(TrainPlan trainPlan) {
        QueryWrapper<TrainPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0);
        queryWrapper.and(temp -> temp.eq("is_pass", 0).or().eq("is_pass", -1).or().eq("is_pass", 1));
        queryWrapper.and(temp -> temp.eq("is_submit", 0).or().eq("is_submit", -1).or().eq("is_submit", 1));

        List<TrainPlan> trainPlans = trainPlanService.list(queryWrapper);
        // 将dict_code转化为标签
        List<TrainPlan> res = new ArrayList<>();
        for (TrainPlan trainPlanVo1 : trainPlans) {
            TrainPlan temp = new TrainPlan();
            BeanUtils.copyProperties(trainPlanVo1, temp);
            String qualifiedType = trainPlanVo1.getQualifiedType();
            String workKind = trainPlanVo1.getWorkKind();
            String operationType = trainPlanVo1.getOperationType();
            SysDictData dictData1 = dictDataService.selectDictDataById(Long.parseLong(qualifiedType));
            SysDictData dictData2 = dictDataService.selectDictDataById(Long.parseLong(workKind));
            if (operationType != null && !operationType.equals("")  && operationType.length() > 0) {
                SysDictData dictData3 = dictDataService.selectDictDataById(Long.parseLong(operationType));
                temp.setOperationType(dictData3.getDictLabel());
            }
            temp.setQualifiedType(dictData1.getDictLabel());
            temp.setWorkKind(dictData2.getDictLabel());

            res.add(temp);
        }
        return res;
    }

/*    // 得到级联选择器
    @Override
    public List<DictVo> getCascadeSelector() {
        List<DictVo> dictVoList = new ArrayList<>();
        List<SysDictData> sysDictData = dictDataService.selectDictDataByPid(0L);
        for (SysDictData dictData : sysDictData) {
            DictVo dictVo = new DictVo();
            dictVo.setLabel(dictData.getDictLabel());
            dictVo.setValue(dictData.getDictCode());
            List<SysDictData> DictDataChildren1 = dictDataService.selectDictDataByPid(dictData.getDictCode());

            List<DictVo> dictVoChildren1 = new ArrayList<>();
            for (SysDictData dictData1 : DictDataChildren1) {
                DictVo dictVo1 = new DictVo();
                dictVo1.setLabel(dictData1.getDictLabel());
                dictVo1.setValue(dictData1.getDictCode());
                List<DictVo> dictVoChildren2 = new ArrayList<>();
                List<SysDictData> sysDictData1 = dictDataService.selectDictDataByPid(dictData1.getDictCode());
                for (SysDictData sysDictData2 : sysDictData1) {
                    DictVo dictVo2 = new DictVo();
                    dictVo2.setValue(sysDictData2.getDictCode());
                    dictVo2.setLabel(sysDictData2.getDictLabel());
                    dictVoChildren2.add(dictVo2);
                }

                if (dictVoChildren2 != null && dictVoChildren2.size() != 0) dictVo1.setChildren(dictVoChildren2);
                dictVoChildren1.add(dictVo1);
            }


            if (dictVoChildren1 != null && dictVoChildren1.size() != 0) dictVo.setChildren(dictVoChildren1);

            dictVoList.add(dictVo);
        }

        return dictVoList;
    }*/

    // 通过pid得到信息
    @Override
    public List<DictVo> getDictDateByPid(Long pid) {
        List<SysDictData> sysDictData = dictDataService.selectDictDataByPid(pid);
        List<DictVo> dictVoList = new ArrayList<>();
        for (SysDictData sysDictData1 : sysDictData) {
            DictVo dictVo = new DictVo();
            dictVo.setValue(sysDictData1.getDictCode());
            dictVo.setLabel(sysDictData1.getDictLabel());
            dictVoList.add(dictVo);
        }
        return dictVoList;
    }
}

